<template>
    <div>
        <!--查询条件start --111s-->
        <el-card>


        <el-form :inline="true"
                 :model="query"
                 class="query-form"
                 size="small">
            <el-form-item>
                <el-button type="primary"
                           icon="el-icon-edit"
                           @click.native="addBrandCommodity()">添加
                </el-button>
            </el-form-item>

            <el-form-item class="query-form-item">
                <el-input v-model="query.content"
                          placeholder="请输入商品名称"
                          prefix-icon="el-icon-search"
                          @keyup.enter.native="onSubmit"
                          clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary"
                           icon="el-icon-refresh"
                           @click="onRest"></el-button>
                <el-button type="primary"
                           icon="el-icon-search"
                           @click="onSubmit">查询</el-button>
            </el-form-item>

        </el-form>
        <!--查询条件end-->
        <!--列表start --->
        <el-table ref="multipleTable"
                  v-loading="loading"
                  :data="list"
                  :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                  @selection-change="handleSelectionChange"
                  border>
            <el-table-column label="序号" prop="commodityname" width="50" align="center" fixed>
                <template slot-scope="scope">
                    <span>{{scope.$index + 1}}</span>
                </template>
            </el-table-column>
            <el-table-column label="商品名称" prop="commodityname" width="150" align="center"/>
            <el-table-column label="轮播图"
                             prop="commodityicon"
                             align="center"
                             width="180">
                <template slot-scope="scope">
                    <el-popover v-if="scope.row.commodityicon"
                                placement="right"
                                trigger="hover">
                        <img :src="scope.row.commodityicon"
                             style="width:400px;border-radius:3px;">
                        <img slot="reference"
                             :src="scope.row.commodityicon"
                             style="max-height: 80px;max-width: 50px;border-radius:5px;">
                    </el-popover>
                    <span v-else>-</span>
                </template>
            </el-table-column>
            <el-table-column label="一级分类" prop="topcategoryname" width="150" align="center"/>
            <el-table-column label="二级分类" prop="secondarycategoryname" width="150" align="center"/>
            <el-table-column label="三级分类" prop="thirdcategoryname" width="150" align="center"/>
            <el-table-column label="审核状态" with="100" align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.commoditystatus == 0">审核中</span>
                    <span v-if="scope.row.commoditystatus == 1">审核通过</span>
                    <span v-if="scope.row.commoditystatus == 2">审核拒绝</span>
                    <div v-if="scope.row.commoditystatus == 2">
                        <el-button type="text" @click="openAlert(scope.row)">查看</el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="库存" prop="commoditynum" width="150" align="center"/>
            <el-table-column label="上架状态" prop="onshelfstatus" align="center" width="120">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.onshelfstatus"
                        active-color="#13ce66"
                        inactive-color="#ff4949"
                        active-value='1'
                        inactive-value='0'
                        @change="onshelfschange(scope.row)"
                    >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="添加时间"
                             with="300"
                             :show-overflow-tooltip="true"
                             align="center">
                <template slot-scope="scope">
                    <i class="el-icon-time"
                       v-if="scope.row.createtime!=null"></i>
                    <span v-if="scope.row.createtime!=null">{{ scope.row.createtime | formatDateStr('yyyy-MM-dd hh:mm:ss') }}</span>
                    <span v-else>无</span>
                </template>
            </el-table-column>

            <el-table-column label="操作"
                             align="center" width="250" fixed="right">
                <template slot-scope="scope">
                    <el-button type="primary"
                               size="mini"
                               @click.native="detailForm(scope.$index, scope.row)">查看</el-button>
                    <el-button type="primary" size="mini" @click.native="editForm(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="primary" size="mini" @click.native="inventoryForm(scope.$index, scope.row)">库存</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--列表end --222e-->

        <!--分页模块start-->
        <el-pagination class="pagination-container"
                       background
                       :page-size="query.pageSize"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       layout="total,sizes,prev, pager, next, jumper"
                       :current-page="query.currentPage"
                       :page-sizes="[5,10, 20, 30, 50]"
                       :total="totalNumber"></el-pagination>
        <!--分页模块end-->

        <!--新增/编辑弹框start--333s-->
        <el-dialog :title="dialogAddOrEditName"
                   :visible.sync="dialogAddOrEdit"
                   width="50%"
                   :before-close="hideDialogAddOrEdit"
                   :close-on-click-modal="false">
            <template slot="title">
                <div class="avue-crud__dialog__header">
                    <span class="el-dialog__title">
                    <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                    {{dialogAddOrEditName}}
                    </span>
                </div>
            </template>
            <el-card>
            <el-form :model="formData"  :rules="rules"  ref="ruleForm" label-width="100px"  class="demo-ruleForm">
                <el-form-item label="商户" prop="userId">
                    <el-select v-model="formData.userId" placeholder="请选择" clearable @change="getMerchantsGoods()">
                        <el-option v-for="item in merchantslist"  :key="item.userId" :label="item.usernick"  :value="item.userId">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!--列表start --->
                <el-table ref="merchantsgoodslist"
                          v-loading="checkLoading"
                          :data="merchantsgoodslist"
                          :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                          @selection-change="checkHandleSelectionChange"
                          border>
                    <el-table-column type="selection"  width="50" fixed />
                    <el-table-column label="ID"  prop="commodityid" width="100" align="center" />
                    <el-table-column label="商品图片" prop="commodityicon" align="center" width="200">
                        <template slot-scope="scope">
                            <el-popover v-if="scope.row.commodityicon"  placement="right" trigger="hover">
                                <img :src="scope.row.commodityicon"  style="width:400px;border-radius:3px;">
                                <img slot="reference"  :src="scope.row.commodityicon" style="max-height: 80px;max-width: 50px;border-radius:5px;">
                            </el-popover>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="商品名称" prop="commodityname"  align="center" />
                    <el-table-column label="添加时间" prop="createtime" width="200"  align="center">
                        <template slot-scope="scope">
                            <span v-if="scope.row.createtime!=null">{{ scope.row.createtime | formatDateStr('yyyy-MM-dd hh:mm:ss') }}</span>
                            <span v-else>无</span>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页模块start-->
                <!-- <el-pagination class="pagination-container"
                               background
                               :page-size="checkQuery.pageSize"
                               @size-change="checkHandleSizeChange"
                               @current-change="checkHandleCurrentChange"
                               layout="total,sizes,prev, pager, next, jumper"
                               :current-page="checkQuery.currentPage"
                               :page-sizes="[5,10, 20, 30, 50]"
                               :total="checkTotalNumber"></el-pagination> -->
            </el-form>
            </el-card>
            <span slot="footer"  class="dialog-footer">
                <el-button @click="closeCheck">取 消</el-button>
                <el-button type="primary" :disabled="merchantsgoodslist.length == 0"  :loading="formSumbitLoding" @click="formSumbit">提 交</el-button>
            </span>
        </el-dialog>

        <!--查看弹框-->
        <el-dialog
            title="查看"
            :visible.sync="detailVisible"
            :before-close="hideDetailForm"
            width="50%"
            top="5vh"
            :close-on-click-modal="false" 
            v-dialogDrag
        >
        <template slot="title">
                <div class="avue-crud__dialog__header">
                    <span class="el-dialog__title">
                    <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                    {{formMap[formName]}}
                    </span>
                    <div class="avue-crud__dialog__menu" >
                        <icon-svg icon-class="quanping" />
                        <span  class="mention">双击全屏</span>
                    </div>
                </div>
            </template>

            <el-card>
            <el-form :model="formData" label-width="140px">
                <el-form-item label-width="80px" label="基本参数" style="background: #e1e1e1">
                </el-form-item>
                <el-form-item label="商品名称:">
                    <input v-model="formData.commodityname" type="text" readonly>
                </el-form-item>
                <el-form-item label="商品分类:">
                    <span>{{formData.topcategoryname+","+formData.secondarycategoryname+","+formData.thirdcategoryname}}</span>
                </el-form-item>

                <el-form-item style="border-bottom:1px solid #ebeef5;" label-width="0px">

                    <el-form-item label="代理价:" style="width:30%;display: inline-block;">
                        <input v-model="formData.commodityoldprice" type="text" readonly>
                    </el-form-item>
                    <el-form-item label="零售价:" style="margin-left:80px; width:30%;display: inline-block;">
                        <input v-model="formData.commodityprice" type="text" readonly>
                    </el-form-item>

                </el-form-item>

                <!-- <el-form-item style="border-bottom:1px solid #ebeef5;" label-width="0px">

                    <el-form-item label="快递:" style="width:30%;display: inline-block;">
                        <span>{{formData.postagetype=='0'? '包邮':'不包邮'}}</span>
                    </el-form-item>
                    <el-form-item label="发货地:" style="margin-left:80px; width:30%;display: inline-block;">
                        <input v-model="formData.commodityfrom" type="text" readonly>
                    </el-form-item>

                </el-form-item> -->

                <el-form-item style="border-bottom:1px solid #ebeef5;" label-width="0px">
                    <el-form-item label="添加时间:" style="width:30%;display: inline-block;">
                        <input
                            :value="formData.createtime |formatDateStr('yyyy-MM-dd') "
                            type="text"
                            readonly
                        >
                    </el-form-item>
                    <el-form-item label="商品ID:" style="margin-left:80px; width:30%;display: inline-block;">
                        <input v-model="formData.commodityid" type="text" readonly>
                    </el-form-item>

                </el-form-item>

                <el-form-item style="border-bottom:1px solid #ebeef5;" label-width="0px">
                    <el-form-item label="封面图:" style="width:30%;display: inline-block">
                        <el-popover v-if="formData.commodityicon" placement="right" trigger="hover">
                            <img :src="formData.commodityicon" style="width:400px;border-radius:3px;">
                            <img
                                slot="reference"
                                :src="formData.commodityicon"
                                style="max-height: 80px;max-width: 50px;border-radius:5px;"
                            >
                        </el-popover>
                        <span v-else>-</span>

                    </el-form-item>

                    <el-form-item label="库存:" style="margin-left:80px; width:30%;display: inline-block;">
                        <input v-model="formData.commoditynum" type="text" readonly>
                    </el-form-item>
                </el-form-item>
                <el-form-item label-width="120px" label="商品资质认证" style="background: #e1e1e1">
                </el-form-item>
                <h3 style="height: 50px;" class="thrTitle">{{categoryTemplate.name || ""}}</h3>
                <el-form-item label-width="150px" :label="item.fieldname+'：'" :prop="'fieldname'+index" v-for="(item,index) in categoryTemplate.templateItemFormList" :key="item.id">
                    <input type="hidden" v-model="item.fieldvalue">
                    <el-input v-model="formData4[item.fieldname]" @input="onInput()" @change="changeInput(item)" v-if="item.fieldtype == '文字'" type="text" :placeholder="'请填写'+item.fieldname" disabled></el-input>
                    <el-input v-model="formData4[item.fieldname]" @input="onInput()" @change="changeInput(item)" v-if="item.fieldtype == '数字'" type="number" :placeholder="'请填写'+item.fieldname" disabled></el-input>
                    <el-popover v-if="item.fieldtype == '图片'" placement="right" trigger="hover">
                        <img :src="item.fieldvalue" style="width:400px;border-radius:3px;">
                        <img slot="reference" :src="item.fieldvalue" style="max-height: 80px;max-width: 150px;border-radius:5px;">
                    </el-popover>
                </el-form-item>
                <el-form-item label-width="80px" label="规格参数" style="background: #e1e1e1">
                </el-form-item>
                <el-table
                    style="margin-bottom: 20px"
                    ref="multipleTable"
                    v-loading="loading"
                    :data="parameterlist"
                    :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                    @selection-change="handleSelectionChange"
                    border
                >
                    <el-table-column label="商品图片" width="100"  align="center">
                        <template slot-scope="scope">
                            <el-popover v-if="scope.row.inventorypic" placement="right" trigger="hover">
                                <img :src="scope.row.inventorypic" style="width:400px;border-radius:3px;">
                                <img
                                    slot="reference"
                                    :src="scope.row.inventorypic"
                                    style="max-height: 80px;max-width: 50px;border-radius:5px;"
                                >
                            </el-popover>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="规格参数" align="center">
                        <el-table-column v-for="(item,index) in inputData" :key="index" :label='item.key' width="150" align="center">
                            <template slot-scope="scope">
                                <div v-if="scope.row.splist!==undefined">
                                    <el-select v-model="scope.row.splist[index]" placeholder="请选择" disabled  ref="sel" >
                                        <el-option
                                            v-for="(it,ind) in (item.val.split(','))"
                                            :key="ind"
                                            :label="it"
                                            :value="it">
                                        </el-option>
                                    </el-select>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table-column>


                    <el-table-column label="成本价" prop="costprice"  align="center"/>
                    <el-table-column label="代理价" prop="primaryagentprice"  align="center"/>
                    <el-table-column label="经销价" prop="secondaryagentprice"  align="center"/>
                    <el-table-column label="分销价" prop="commodityprice"  align="center"/>
                    <el-table-column label="零售价" prop="commodityoldprice" align="center"/>
                    <el-table-column label="县域收益占比(%)" prop="profitproportion" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.profitproportion" align="center" disabled
                                :min="0" :controls="false" :precision="4" ></el-input-number>

                            </template>
                        </el-table-column>
                    <el-table-column label="库存" prop="inventorynum" align="center"/>


                </el-table>
                <el-form-item label-width="65px" label="轮播图" style="background: #e1e1e1;margin-top:20px">
                </el-form-item>
                <el-form-item >

                    <div v-for="(item,index) in formData.commoditypiclist" :key="index" style="display:inline-block;">
                        <img :src="item" style="width:200px;height:auto;border-radius:3px;margin-left: 5px;">
                    </div>


                </el-form-item>
                <el-form-item label-width="80px" label="商品详情" style="background: #e1e1e1"></el-form-item>
                 <span v-html="formData.commoditydesc" style="margin-left:10%;display:block;"></span>


            </el-form>
            </el-card>
        </el-dialog>
        <!--库存弹框-->
        <el-dialog
            :title="formMap[formName]"
            :visible.sync="inventoryVisible"
            :before-close="hideInventoryForm"
            width="50%"
            top="5vh"
            v-dialogDrag
        >
          <template slot="title">
                <div class="avue-crud__dialog__header">
                    <span class="el-dialog__title">
                    <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                    {{formMap[formName]}}
                    </span>
                    <div class="avue-crud__dialog__menu" >
                        <icon-svg icon-class="quanping" />
                        <span  class="mention">双击全屏</span>
                    </div>
                </div>
            </template>
            <el-card>


            <el-form :model="formData" label-width="140px">

                <el-form-item label="商品名称:">
                    <input v-model="formData.commodityname" type="text" readonly>
                </el-form-item>
                <el-table
                    ref="multipleTable"
                    v-loading="loading"
                    :data="parameterlist"
                    :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                    @selection-change="handleSelectionChange"
                    border
                >
                    <el-table-column label="商品图片" width="100"  align="center">
                        <template slot-scope="scope">
                            <el-popover v-if="scope.row.inventorypic" placement="right" trigger="hover">
                                <img :src="scope.row.inventorypic" style="width:400px;border-radius:3px;">
                                <img slot="reference" :src="scope.row.inventorypic" style="max-height: 80px;max-width: 50px;border-radius:5px;">
                            </el-popover>
                            <span v-else>-</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="规格参数" align="center">
                        <el-table-column v-for="(item,index) in inputData" :key="index" :label='item.key' width="150" align="center">
                            <template slot-scope="scope">
                                <div v-if="scope.row.splist!==undefined">
                                    <el-select v-model="scope.row.splist[index]" placeholder="请选择" disabled  ref="sel" >
                                        <el-option
                                            v-for="(it,ind) in (item.val.split(','))"
                                            :key="ind"
                                            :label="it"
                                            :value="it">
                                        </el-option>
                                    </el-select>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table-column>
                    <el-table-column label="成本价" prop="costprice"  align="center"/>
                    <el-table-column label="代理价" prop="primaryagentprice"  align="center"/>
                    <el-table-column label="经销价" prop="secondaryagentprice"  align="center"/>
                    <el-table-column label="分销价" prop="commodityprice"  align="center"/>
                    <el-table-column label="零售价" prop="commodityoldprice" align="center"/>
                    <el-table-column label="库存" prop="commoditynum"  width="210" align="center"  >
                        <template slot-scope="scope">
                            <el-input-number :min="0" :max="99999" v-model="scope.row.inventorynum" controls-position="right" placeholder="请输入商品库存" clearable ></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column label="起购数量" prop="buyfromnum" width="200" align="center">
                        <template slot-scope="scope">
                            <el-input-number v-model="scope.row.buyfromnum" controls-position="right" placeholder="起购数量" clearable :min="1" :max="99999"></el-input-number>
                        </template>
                    </el-table-column>
                </el-table>
                <el-form-item>
                    <div align="center" style="margin: 10px">
                         <el-button @click.native="hideInventoryForm">取消</el-button>
                        <el-button type="primary" @click="inventoryFormSubmit">提交</el-button>
                    </div>
                </el-form-item>
            </el-form>
              </el-card>
        </el-dialog>


        <!--add新建编辑-->
        <el-dialog
            :title="formMap[formName]"
            :visible.sync="formVisible"
            :before-close="hideForm"
            width="50%"
            top="5vh"
            v-dialogDrag
            :close-on-click-modal="false"
        >
        <template slot="title">
            <div class="avue-crud__dialog__header">
                <span class="el-dialog__title">
                <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                {{formMap[formName]}}
                </span>
            </div>
        </template>
            <el-card>

            <el-form :model="formData" :rules="formRules" ref="ruleForm">
                <el-form-item label-width="80px"  label="基本参数" style="background: #e1e1e1">
                </el-form-item>
                <el-form-item label-width="120px"  label="商品名称:" prop="commodityname">
                    <el-input style="width: 50%" v-model="formData.commodityname" placeholder="请输入商品名称" clearable></el-input>
                </el-form-item>
                <el-form-item label-width="120px" label="商品分类:" prop="thirdcategoryid">
                   <span style="margin-right: 10px;">{{
                  oneCategoryObj.topcategoryname +
                  " --> " +
                  twoCategoryObj.topcategoryname  + " --> "

              }}
          </span>
                    <el-select v-model="formData.thirdcategoryid" placeholder="请选择" clearable @change="thirdchange1(2)" disabled>
                        <el-option v-for="item in busCommodityThirdCategoryList"
                                   :key="item.topcategoryid"
                                   :label="item.topcategoryname"
                                   :value="item.topcategoryid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <!--商品资质认证-->
                <el-form-item label-width="120px" label="商品资质认证" style="background: #e1e1e1">
                </el-form-item>
                <span v-if="categoryTemplate != [] && categoryTemplate != null">
                    <h3 style="height: 50px" class="thrTitle">{{categoryTemplate.name || ""}}</h3>
                    <el-form-item label-width="150px" :label="item.fieldname+'：'" :prop="'fieldname'+index" v-for="(item,index) in categoryTemplate.templateItemFormList" :key="item.id">
                        <input type="hidden" v-model="item.fieldvalue">
                        <el-input v-model="formData4[item.fieldname]" @input="onInput()" @change="changeInput(item)" v-if="item.fieldtype == '文字'" type="text" :placeholder="'请填写'+item.fieldname"></el-input>
                        <el-input v-model="formData4[item.fieldname]" @input="onInput()" @change="changeInput(item)" v-if="item.fieldtype == '数字'" type="number" :placeholder="'请填写'+item.fieldname"></el-input>
                        <el-upload
                            v-if="item.fieldtype == '图片'"
                            :action="uploadUrl"
                            :on-success="(response, file,fileList)=> handleFieldSuccess(response, file,fileList,'formData4','fieldname' + index,index)"
                            :on-remove="(file,fileList)=> handleFieldPicRemove(file, fileList,'formData4','fieldname' + index,index)"
                            :on-change="(file)=> handleChange(file)"
                            list-type="picture-card"
                            :show-file-list="false"
                            class="avatar-uploader"
                        >
                        <img v-if="item.fieldvalue" :src="item.fieldvalue" class="avatar" style="width: 100%; height: 100%">
                        <i v-else class="el-icon-plus"></i>
                        </el-upload>
                    </el-form-item>
                </span>
                <!--2-->
                <el-form-item label-width="80px" label="规格参数" style="background: #e1e1e1">
                </el-form-item>
                <el-form-item>
                    <div v-for="(item,index) in inputData" :key="index">
                        <el-form-item style=" width:30%;display: inline-grid;">
                            <div style="float: none">
                                <el-tag>{{item.key}}</el-tag>
                                <el-input
                                    v-model="item.val"
                                    placeholder="请输入内容"
                                    clearable
                                    size="small"
                                />
                            </div>

                        </el-form-item>
                    </div>
                </el-form-item>

                <!--3-->
                <el-form-item label-width="55px" label="库存" style="background: #e1e1e1">
                    <span>{{formData.commoditynum}}</span>
                </el-form-item>

                <el-form-item>
                    <el-table
                        ref="multipleTable"
                        v-loading="loading"
                        :data="parameterlist"
                        :header-cell-style="{background:'#F4F4F5',color:'#606266'}"
                        @selection-change="handleSelectionChange"
                        border
                    >
                        <el-table-column type="selection" width="50" fixed/>
                        <el-table-column label="商品图片" width="100"
                                         align="center">

                            <template slot-scope="scope">

                                <el-upload
                                    ref="upload"
                                    class="avatar-uploader"
                                    :data="uploadData1"
                                    :action="uploadUrl"
                                    :show-file-list="false"
                                    :on-success="handleAvatarSuccess"
                                >
                                    <img v-if="scope.row.inventorypic" :src="scope.row.inventorypic" class="avatar"
                                         style="width: 50px;height: 50px">
                                    <i v-else class="el-icon-plus avatar-uploader-icon"
                                       @click="curRowIndex=scope.$index" style="line-height: 2.3"></i>
                                </el-upload>

                            </template>

                        </el-table-column>
                        <el-table-column label="规格参数" align="center">
                            <div v-for="(item,index) in inputData" :key="index">
                                <el-table-column :label='item.key' width="150" align="center">
                                    <template slot-scope="scope">
                                        <div v-if="scope.row.splist!==undefined">
                                            <el-select v-model="scope.row.splist[index]" placeholder="请选择" clearable
                                                       @change="paramchange($event,scope.$index,item)" >
                                                <el-option
                                                    v-for="(it,ind) in (item.val.split(','))"
                                                    :key="ind"
                                                    :label="it"
                                                    :value="it">
                                                </el-option>
                                            </el-select>
                                        </div>

                                    </template>

                                </el-table-column>
                            </div>
                        </el-table-column>
                         <el-table-column label="成本价" prop="costprice" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.costprice" align="center" @change="costprice(scope.$index,scope.row)"
                                :min="0" :controls="false" :precision="2"></el-input-number>
                
                            </template>
                        </el-table-column>
                        <el-table-column label="代理价" prop="primaryagentprice" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.primaryagentprice" align="center" @change="primaryagentpriceChange(scope.$index,scope.row)"
                                :min="0" :controls="false" :precision="2"></el-input-number>
                               
                            </template>
                        </el-table-column>
                        <el-table-column label="经销价" prop="secondaryagentprice" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.secondaryagentprice" align="center"
                                :min="0" :controls="false" :precision="2" @change="secondaryagentprice(scope.$index,scope.row)"></el-input-number>
                                
                            </template>
                        </el-table-column>
                        <el-table-column label="分销价" prop="commodityprice" width="150" align="center">
                            <template slot-scope="scope">
                                 <el-input-number style="width: 120px;" v-model="scope.row.commodityprice" align="center"
                                :min="0" :controls="false" :precision="2" @change="commodityprice(scope.$index,scope.row)"></el-input-number>
                                
                            </template>
                        </el-table-column>
                        <el-table-column label="零售价" prop="commodityoldprice" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.commodityoldprice" align="center"
                                :min="0" :controls="false" :precision="2" @change="commodityoldprice(scope.$index,scope.row)"></el-input-number>
                            </template>
                        </el-table-column>
                        <el-table-column label="县域收益占比(%)" prop="profitproportion" width="150" align="center">
                            <template slot-scope="scope">
                                <el-input-number style="width: 120px;" v-model="scope.row.profitproportion" align="center"
                                :min="0" :controls="false" :precision="4" ></el-input-number>
                               
                            </template>
                        </el-table-column>

                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button
                                    type="danger"
                                    size="mini"
                                    @click.native="paraDel(scope.$index,scope.row)"
                                >删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

                    <div align="center" style="margin: 10px;">
                        <el-button type="primary" @click="addpara()">添加</el-button>

                    </div>

                </el-form-item>

                <!--3-->
                <el-form-item label-width="65px" label="轮播图" style="background: #e1e1e1">
                </el-form-item>
                <el-form-item style="display: inline-block">
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
                    <el-upload
                        :action="uploadUrl"
                        :on-change="handleChange"
                        list-type="picture-card"
                        :on-success="handleSuccess"
                        :on-remove="handlePicRemove"
                        :file-list="uploadData"
                    ><i class="el-icon-plus"></i>
                    </el-upload>

                </el-form-item>
                <el-form-item label-width="80px" label="商品详情" style="background: #e1e1e1"></el-form-item>

            </el-form>

            <div>
                <!-- 图片上传组件辅助-->
                <el-upload
                    class="edituploader"
                    :action="uploadUrl"
                    :show-file-list="false"
                    :on-success="imguploadSuccess"
                    :on-error="uploadError"
                    :before-upload="beforeUpload">
                </el-upload>
                <quill-editor
                    v-model="formData.commoditydesc"
                    ref="myQuillEditor"
                    :options="editorOption"
                    @change="onEditorChange($event)"
                >
                </quill-editor>
            </div>
            </el-card>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click.native="formSubmit()"  :loading="formLoading">提交</el-button>
            </div>
        </el-dialog>
        </el-card>
    </div>
</template>
<script>
    import {
        merchantsAgentList,
        getMerchantsGoods,
        addRepertory,
        brandAgentCommodityList,
        updateBrandCommodity,
    } from "../../api/broadcastCommodity/brandAgentCommodity.js";
    // import { updateCommodityMerchantInventory } from "../../api/broadcastCommodity/commodityMerchantInventory";
    import {
        getMainCategoryMap,
        getGoodsCertificationInfo, updateBusCommodity
    } from "../../api/buscommodity/buscommodity.js";
    import { BASE_URL } from "../../config/app";
    import { quillEditor } from "vue-quill-editor"; //调用编辑器
    import "quill/dist/quill.core.css";
    import "quill/dist/quill.snow.css";
    import "quill/dist/quill.bubble.css";
    import {updateBusCommodityInventoryNum} from "../../api/buscommodity/buscommodity";
    /*用作表单的数据初始化*/
    /*用作表单的数据初始化*/
    const formJson = {
        commodityid: "", //商品id
        commodityname: "", //商品名称
        recommendstatus: 0, //商品'0不推荐1推荐',
        onshelfstatus: 1, //商品 0全部，  1不上架 2上架',
        commodityicon: "", //商品图片
        commodityprice: "", // 售价    decimal(20,2),
        commodityoldprice: "", //商品原价    decimal(20,2),
        commodityfrom: "", // 货源地
        topcategoryname: "", //一级分类
        secondarycategoryname: "", //二级分类
        thirdcategoryname: "", //三级分类
        commoditynum: "", // 库存
        createtime: "", // 添加时间
        commoditydesc: "", //商品描述
        commoditypiclist: [], //图片
        piclist: "", //图片列表
        videopic: "", //视频地址

        /**/
        topcategoryid: "", //一级分类id',
        secondarycategoryid: "", //二级分类id',
        thirdcategoryid: "", // 三级分类id,

        inids: "", //参数id
        param: "",
        inputdata: "", //全部参数
        certificationcontent:""
    };
    const toolbarOptions = [
        ["bold", "italic", "underline", "strike"], // toggled buttons
        [{ header: 1 }, { header: 2 }], // custom button values
        [{ list: "ordered" }, { list: "bullet" }],
        [{ indent: "-1" }, { indent: "+1" }], // outdent/indent
        [{ direction: "rtl" }], // text direction
        [{ size: ["small", false, "large", "huge"] }], // custom dropdown
        [{ header: [1, 2, 3, 4, 5, 6, false] }],
        [{ color: [] }, { background: [] }], // dropdown with defaults from theme
        [{ font: [] }],
        [{ align: [] }],
        ["link", "image"],
        ["clean"]
    ];

    export default {
        components: {
            quillEditor
        },
        data() {
            return {
                categoryTemplate: [], // 商品类目模板
                formLoading: false, //表单提交加载
                uploadUrl: BASE_URL + '/goldendays-currency/tool/uploadpro', //上传图片
                uploadvideo: BASE_URL + "/tool/uploadVideo", //上传视频
                uploadData: [],
                vuploadData: [], //视频
                uploadData1: {}, // 图片data
                curRowIndex: 0, //图片row
                createTime: [], //选择日期时需要的model
                query: {
                    //查询条件
                    content: "",
                    starttime: "",
                    endtime: "",
                    onshelfstatus: "0", //0全部，  1不上架 2上架
                    recommendstatus: "", //0不推荐1推荐
                    currentPage: 1,
                    pageSize: 10,
                    thirdcategoryid: "" //三级
                },
                checkQuery: {
                    currentPage: 1,
                    pageSize: 10
                },
                commodityIdArr: [],
                checkTotalNumber: 0, //分页总页数
                formName: null,
                formMap: {
                    add: "添加",
                    edit: "编辑",
                    detail: "查看",
                    inventoryEdit: "库存管理"
                },
                inventoryVisible: false, //库存管理编辑显示
                formRules: {
                    commodityname: {
                        required: true,
                        message: "请输入名称",
                        trigger: "blur"
                    },
                    thirdcategoryid: {
                        required: true,
                        message: "请选择分类",
                        trigger: "blur"
                    },
                    /*commodityprice: {
                        required: true,
                        message: "请输入售价",
                        trigger: "blur"
                    },
                    commodityoldprice: {
                        required: true,
                        message: "请输入代理价",
                        trigger: "blur"
                    },*/
                    commoditydesc: {
                        required: true,
                        message: "请输入商品详情",
                        trigger: "blur"
                    }
                },
                merchantslist: [],
                merchantsgoodslist: [],
                list: [], //列表集合
                parameterlist: [], //add参数list

                //savadd参数list
                category: [], //分类列表
                buswarehouse: [], //仓库列表
                busCommoditySecondaryCategoryList: [], //二级
                busCommodityThirdCategoryList: [], //三级
                categoryobj: {}, //分类列表
                SecondaryCategoryobj: {}, //二级
                ThirdCategoryobj: {}, //三级
                inputData: [], // 循环创建input存值
                totalNumber: 0, //分页总页数
                formVisible: false, //添加编辑显示
                dis: true, //不可点击
                loading: true, //列表加载
                checkLoading: false,
                formSumbitLoding: false, //表单提交加载
                cancelToggle: false, //列表是否全选
                multipleSelection: [], //多选集合
                dialogAddOrEdit: false, //新增或编辑弹框开关
                dialogAddOrEditName: "", //新增或编辑弹框名字
                formData: formJson, //新增或修改的对象
                rules: {
                    //表单校验
                    userId: {
                        required: true,
                        message: "请选择商户",
                        trigger: "blur"
                    },

                    ids: {
                        required: true,
                        message: "请选择商品",
                        trigger: "blur"
                    }
                },
                oneCategoryObj:{},
                twoCategoryObj:{},
                detailVisible: false, // 查看窗口
                showPrice:1, // 商品详情显示价格  1 显示1及代理价  其余显示2级代理价
                formData4: {
                    // 第四步表单
                    storename: null, // 店铺名称
                    enterinfoid: null, // 主键
                    certificationcontent: null, // 商品分类模板
                    topcategoryid: null, // 一级商品分类id
                    secondarycategoryid: null, // 二级商品分类id
                    categoryid: null // 三级商品分类id
                },
                quillUpdateImg: false, // 根据图片上传状态来确定是否显示loading动画，刚开始是false,不显示
                // content: null,
                editorOption: {
                    placeholder: "",
                    theme: "snow", // or 'bubble'
                    modules: {
                        toolbar: {
                            container: toolbarOptions,
                            handlers: {
                                image: function(value) {
                                    if (value) {
                                        // 触发input框选择图片文件
                                        document
                                            .querySelector(".edituploader input")
                                            .click();
                                    } else {
                                        this.quill.format("image", false);
                                    }
                                }
                            }
                        }
                    }
                },
            };
        },

        methods: {
            handleSelectionChange(){

            },
            checkHandleSelectionChange(){

            },
            checkHandleSizeChange(){

            },
            checkHandleCurrentChange(){

            },
            hideForm(){
              this.formVisible =false
            },
            handlePicRemove(){

            },
            imguploadSuccess(){

            },
            uploadError(){

            },
            beforeUpload(){

            },
            handleAvatarSuccess(){},
            handleAvatarSuccess(){},
            // 隐藏库存管理
            hideInventoryForm() {
                // 更改值
                this.inventoryVisible = !this.inventoryVisible;
                return true;
            },
            costprice(index,row){
                if(row.primaryagentprice!=null&&row.primaryagentprice!=""&&row.costprice!=null&&row.costprice!=""){
                    var primaryagentprice=row.primaryagentprice-0;
                    var costprice=row.costprice-0;
                    if(primaryagentprice<=costprice){
                        this.$message.error("成本价不能高于于代理价价,请重新输入!");
                        row.costprice = "";
                        return false;
                    }
                }
            },
            primaryagentpriceChange(index,row){
                if(row.primaryagentprice!=null&&row.primaryagentprice!=""&&row.costprice!=null&&row.costprice!=""){
                    var primaryagentprice=row.primaryagentprice-0;
                    var costprice=row.costprice-0;
                    if(primaryagentprice<=costprice){
                        this.$message.error("代理价不能低于成本价,请重新输入!");
                        row.primaryagentprice = "";
                        return false;
                    }
                }
                if(row.primaryagentprice!=null&&row.primaryagentprice!=""&&row.secondaryagentprice!=null&&row.secondaryagentprice!=""){
                    var primaryagentprice1=row.primaryagentprice-0;
                    var secondaryagentprice=row.secondaryagentprice-0;
                    if(secondaryagentprice<=primaryagentprice1){
                        this.$message.error("代理价不能高于经销价,请重新输入!");
                        row.primaryagentprice = "";
                        return false;
                    }
                }
            },
            secondaryagentprice(index,row){
                if(row.primaryagentprice!=null&&row.primaryagentprice!=""&&row.secondaryagentprice!=null&&row.secondaryagentprice!=""){
                    var primaryagentprice=row.primaryagentprice-0;
                    var secondaryagentprice=row.secondaryagentprice-0;
                    if(secondaryagentprice<=primaryagentprice){
                        this.$message.error("经销价不能低于代理价,请重新输入!");
                        row.secondaryagentprice = "";
                        return false;
                    }
                }
                if(row.commodityprice!=null&&row.commodityprice!=""&&row.secondaryagentprice!=null&&row.secondaryagentprice!=""){
                    var commodityprice=row.commodityprice-0;
                    var secondaryagentprice1=row.secondaryagentprice-0;
                    if(commodityprice<=secondaryagentprice1){
                        this.$message.error("经销价不能高于分销价,请重新输入!");
                        row.secondaryagentprice = "";
                        return false;
                    }
                }
            },
            commodityprice(index,row){
                if(row.commodityprice!=null&&row.commodityprice!=""&&row.secondaryagentprice!=null&&row.secondaryagentprice!=""){
                    var commodityprice=row.commodityprice-0;
                    var secondaryagentprice=row.secondaryagentprice-0;
                    if(commodityprice<secondaryagentprice){
                        this.$message.error("分销价不能低于经销价,请重新输入!");
                        row.commodityprice = "";
                        return false;
                    }
                }
                if(row.commodityprice!=null&&row.commodityprice!=""&&row.commodityoldprice!=null&&row.commodityoldprice!=""){
                    var commodityprice1=row.commodityprice-0;
                    var commodityoldprice=row.commodityoldprice-0;
                    if(commodityoldprice<commodityprice1){
                        this.$message.error("分销价不能高于零售价,请重新输入!");
                        row.commodityprice = "";
                        return false;
                    }
                }
            },
            commodityoldprice(index,row){
                if(row.commodityprice!=null&&row.commodityprice!=""&&row.commodityoldprice!=null&&row.commodityoldprice!=""){
                    var commodityprice=row.commodityprice-0;
                    var commodityoldprice=row.commodityoldprice-0;
                    if(commodityoldprice<commodityprice){
                        this.$message.error("零售价不能低于分销价,请重新输入!");
                        row.commodityoldprice = "";
                        return false;
                    }
                }
            },
            //图片上传成功
            handleFieldSuccess(response, file, fileList, index, key, item) {
                if (response.status === 1) {
                    this[index][key] = response.data;
                    if (item || item == 0) {
                        // item.fieldvalue = this.formData4[item.fieldname];
                        this.categoryTemplate.templateItemFormList[
                            item
                            ].fieldvalue = this.formData4[key];
                        console.log(
                            this.categoryTemplate.templateItemFormList[item]
                        );
                        console.log(this.formData4);
                    }
                    return;
                }
                this.$message.error("上传文件报错");
            },
            //提交库存修改数据
            inventoryFormSubmit() {
                console.log('---更新后的库存'+ JSON.stringify(this.parameterlist));
                let plist = this.parameterlist;
                for (let i = 0; i < plist.length; i++) {
                    if (plist[i].inventorynum==null||plist[i].inventorynum=='') {
                        this.$message.warning("库存不能为空");
                        return false;
                    }
                    if (plist[i].inventorynum < 0) {
                        this.$message.warning("库存不能小于0");
                        return false;
                    }
                    if (plist[i].buyfromnum==null||plist[i].buyfromnum=='') {
                        this.$message.warning("起购不能为空");
                        return false;
                    }
                    if (plist[i].buyfromnum < 1 || plist[i].buyfromnum=='') {
                        this.$message.warning("起购不能小于1");
                        return false;
                    }
                }

                let paramsData=[];
                for (let i = 0; i < plist.length; i++) {
                    let inventoryObj={
                        inventoryId:"",
                        inventoryNum:"",
                        commodityId: '',
                        buyfromnum: '',
                    }
                    inventoryObj.inventoryId  = plist[i].inventoryid;
                    inventoryObj.inventoryNum = plist[i].inventorynum;
                    inventoryObj.buyfromnum = plist[i].buyfromnum;
                    inventoryObj.commodityId = this.formData.commodityid
                    paramsData.push(inventoryObj);
                }
                var params = new URLSearchParams();
                params.append("paramsData", JSON.stringify(paramsData));
                console.log('库存更新提交的数据：'+ JSON.stringify(paramsData));
                updateBusCommodityInventoryNum(params).then((response) => {
                    this.formLoading = true;
                    if (response.code != 0) {
                        this.$message.error(response.message);
                        return false;
                    }
                    this.$message.success("操作成功");

                    this.formData = {};
                    this.formLoading = false;
                    this.inventoryVisible = false;
                    this.dis = true;
                    // 加载表格数据
                    // this.platformCommodityList();
                    this.brandCommodityList();
                }).catch(() => {
                    this.formLoading = false;
                    this.inventoryVisible = false;
                    this.dis = true;
                });
            },
            // 移除图片
            handleFieldPicRemove(file, fileList, index, key, item) {
                this[index][key] = null;
                if (item || item == 0) {
                    // item.fieldvalue = null;
                    this.categoryTemplate.templateItemFormList[
                        item
                        ].fieldvalue = null;
                    console.log(this.categoryTemplate.templateItemFormList[item]);
                    console.log(this.formData4);
                }
            },
            arrNumber(arr){
                var arr_number = {};
                for(var i = 0 ; i < arr.length ; i++){
                    if(arr_number[arr[i]]){
                        arr_number[arr[i]]++
                    }
                    else{
                        arr_number[arr[i]] = 1 ;
                    }
                }
                return arr_number ;
            },
            // 提交
            formSubmit() {
                console.log(this.parameterlist);
                let plist = this.parameterlist;

                for (let i = 0; i < plist.length; i++) {
                    let jsonval = JSON.stringify(plist[i].savparameterlist);
                    let json = plist[i].savparameterlist;
                    let indata = {};
                    for (var j = 0, l = json.length; j < l; j++) {
                        for (var key in json[j]) {
                            // alert(key+':'+json[j][key]);
                            this.$set(indata, key, json[j][key]);
                        }
                    }
                    plist[i].inventory = JSON.stringify(indata);
                    plist[i].secinventory = jsonval;
                }

                //表单校验通过
                this.$refs["ruleForm"].validate(valid => {
                    if (valid) {
                        if (
                            this.formData.commoditypiclist &&
                            this.formData.commoditypiclist.length !== 0
                        ) {
                            this.formData.commodityicon = this.formData.commoditypiclist[0];
                            this.formData.piclist = this.formData.commoditypiclist.join(
                                ","
                            );
                        }
                        var inventorylistobj=[];
                        if (plist) {
                            let num = 0;
                            for (let i = 0; i < plist.length; i++) {
                                let nm = 1;
                                inventorylistobj.push(plist[i].inventory);
                                var inventory=  plist[i].inventory.substring(1,plist[i].inventory.length-1);
                                var inventorylist=inventory.split(":");
                                if(inventorylist[1].length<3){
                                    this.$message.warning("请完善商品规格信息！");
                                    return false;
                                }
                                if(plist[i].costprice<1){
                                    this.$message.warning("请完善商品规格信息！");
                                    return false;
                                }
                                if(plist[i].commodityprice <1){
                                    this.$message.warning("请完善商品规格信息！");
                                    return false;
                                }

                                if(plist[i].commodityoldprice <1){
                                    this.$message.warning("请完善商品规格信息！");
                                    return false;
                                }
                                if(plist[i].secondaryagentprice <1){
                                    this.$message.warning("请完善商品规格信息！");
                                    return false;
                                }
                                num += plist[i].inventorynum * nm;
                            }

                            this.formData.commoditynum = num;
                        }else{
                            this.$message.warning("无商品规格信息！");
                            return false;
                        }
                        this.formData.commodityprice = plist[0].commodityoldprice;
                        this.formData.commodityoldprice = plist[0].primaryagentprice;
                        var inventoryojb= this.arrNumber(inventorylistobj);
                        console.info(inventoryojb)
                        for (let i = 0; i < plist.length; i++) {
                            var inventorycount=  inventoryojb[plist[i].inventory]
                            console.info(inventoryojb[plist[i].inventory])
                            if(inventorycount>1){
                                this.$message.warning("商品不可添加俩个及以上的相同规格！");
                                return false;
                            }
                        }
                        this.formData.param = JSON.stringify(plist);
                        //alert(JSON.stringify(this.inputData));
                        this.formData.inputdata = JSON.stringify(this.inputData);
                        let flag = false;
                        var msg ="";
                        if(this.categoryTemplate==null ||  this.categoryTemplate==undefined) {
                            this.$message.warning("无商品模板！");
                            return false;
                        }
                        this.formData.certificationcontent = JSON.stringify(this.categoryTemplate);
                        if(this.categoryTemplate.templateItemFormList!=null&&this.categoryTemplate.templateItemFormList!=undefined){
                            this.categoryTemplate.templateItemFormList.forEach(item => {
                                    if((item.fieldvalue==null||item.fieldvalue=="")&& item.ismust==0){
                                        msg = item.fieldname;
                                        flag = true;
                                    }
                                }
                            );
                        }else{
                            this.$message.warning("无商品模板！");
                            return false;
                        }
                        if(flag){
                            this.$message({
                                message: msg+"不能为空！",
                                type: "warning"
                            });
                            return false;
                        }
                        var params = new URLSearchParams(this.formData);

                        updateBrandCommodity(params)
                            .then(response => {
                                this.formLoading = true;
                                if (response.code != 0) {
                                    this.$message.error(response.message);
                                    return false;
                                }
                                this.$message.success("操作成功");
                                this.uploadData = []; //图片
                                this.vuploadData = []; //视频

                                this.formData = {};
                                // this.$refs.tinymce.setContent("");

                                this.formLoading = false;
                                this.formVisible = false;

                                this.dis = true;
                                // 加载表格数据
                                this.brandCommodityList();
                                // 清空表单
                                this.$refs["ruleForm"].resetFields();
                                //取消
                                this.closeCategory();
                            })
                            .catch(() => {
                                this.formLoading = false;
                                this.formVisible = false;
                                this.dis = true;
                            });
                    }
                });
            },
            //关闭分类
            closeCategory() {
                this.classification = false;
                this.formData.topcategoryid = "";
                this.formData.secondarycategoryid = "";
                this.formData.thirdcategoryid = "";

                this.categoryobj = {};
                this.ThirdCategoryobj = {};
                this.SecondaryCategoryobj = {};
                this.inputData = [];
            },
            onEditorChange({ editor, html, text }) {
                console.log(editor);
                console.log(text);
                //内容改变事件
                console.log("---内容改变事件---");
                this.formData.commoditydesc = html;
                console.log(html);
            },
            //图片上传成功
            handleFormDataSuccess(response, file, fileList, index, key, item) {
                if (response.status === 1) {
                    this[index][key] = response.data;
                    if (item) {
                        item.fieldvalue = this.formData4[item.fieldname];
                    }
                    return;
                }
                this.$message.error("上传文件报错");
            },
            // 强制刷新input
            onInput() {
                this.$forceUpdate();
            },
            // 模板input监听
            changeInput(val) {
                val.fieldvalue = this.formData4[val.fieldname];
                console.log(this.formData4);
            },
            //table列表参数
            paramchange(e, i, it) {
                console.info(
                    999999999999 +
                    "=====" +
                    e +
                    "=====" +
                    i +
                    "=====" +
                    JSON.stringify(it)
                );
                for (let j = 0; j < this.parameterlist.length > 0; j++) {
                    for (let i = 0; i < this.parameterlist[j].splist.length; i++) {
                        if (this.parameterlist[j].splist[i] === e) {
                            // this.parameterlist[j].savparameterlist[i]=JSON.stringify(it)
                            let data = {};
                            this.$set(data, it.key, e);
                            this.parameterlist[j].savparameterlist[i] = data;
                            console.info(this.parameterlist[j].savparameterlist);
                        }
                    }
                }
            },
            thirdchange1(type) {
                for (
                    var i = 0;
                    i < this.busCommodityThirdCategoryList.length;
                    i++
                ) {
                    if (this.busCommodityThirdCategoryList[i].topcategoryid == this.formData.thirdcategoryid) {
                        this.ThirdCategoryobj = this.busCommodityThirdCategoryList[
                            i
                            ];
                        if (this.ThirdCategoryobj.specification != null) {
                            let data = this.ThirdCategoryobj.specification.split(
                                `","`
                            );
                            let indata = [];
                            for (let i = 0; i < data.length; i++) {
                                let mydata = {
                                    key: "",
                                    val: ""
                                };
                                mydata.key = data[i];
                                mydata.val = "";
                                indata.push(mydata);
                            }
                            this.inputData = indata;
                        }
                        this.formData.thirdcategoryname = this.busCommodityThirdCategoryList[
                            i
                            ].thirdcategoryname;
                        this.classification = false;
                        break;
                    }
                }
                if(type==2){
                    this.getTemplate(this.formData.thirdcategoryid);
                }

            },
            getTemplate(thirdcategoryid) {
                var _this=this;
                // this.loading = true;
                this.categoryTemplate = [];
                const formData = new FormData();
                formData.append("threeCategoryId", thirdcategoryid);
                getGoodsCertificationInfo(formData)
                    .then(response => {
                        if (response.code) {
                            this.$message.error(response.message);
                            this.loading = false;
                        }
                        if (response.data.templateFromList.length == 0) {
                            this.loading = false;
                            _this.categoryTemplate = [];
                            return false;
                        }
                        this.categoryTemplate = response.data.templateFromList[0];
                        console.info("-----------2"+JSON.stringify(this.categoryTemplate));
                        if(this.categoryTemplate.templateItemFormList!=null &&this.categoryTemplate.templateItemFormList!=undefined) {
                            this.categoryTemplate.templateItemFormList.forEach(
                                item => {
                                    this.formData4[item.fieldname] = null;
                                }
                            );
                        }
                        // setTimeout(() => {
                        //     this.loading = false;
                        // }, 0.3 * 1000);
                    })
                    .catch(err => {
                        this.$message.error(err);
                        this.loading = false;
                    });
            },
            //获得分类
            getAllCategory() {
                this.loading = true;
                getMainCategoryMap()
                    .then(response => {
                        if (response.code) {
                            this.$message.error(response.message);
                        }
                        let data = response.data || [];
                        //this.oneCategoryObj=response.data.one||{};
                        //this.twoCategoryObj=response.data.two||{};
                        //this.busCommodityThirdCategoryList = data.three;


                        this.oneCategoryObj=response.data.one||{};
                        this.twoCategoryObj=response.data.two||{};
                        //this.busCommodityThirdCategoryList = response.data.threeList || [];
                        //this.busCommodityThirdCategoryList = data.three;
                        //this.busCommodityThirdCategoryList = data;
                        this.busCommodityThirdCategoryList = response.data.threeList || [];
                        setTimeout(() => {
                            this.loading = false;
                        }, 0.3 * 1000);
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.totalNumber = 0;
                    });
            },
            //编辑
            editForm(index, row) {
                console.log(index,row,'rowrow')
                this.formVisible = true;
                this.formData = Object.assign({}, formJson);
                if(row.certificationcontent!=null){
                    var ss=Object.assign({}, row);
                    this.categoryTemplate = JSON.parse(ss.certificationcontent);
                    console.log(this.categoryTemplate,'this.categoryTemplate.templateItemFormList')
                    this.formData.certificationcontent = JSON.parse(row.certificationcontent);
                    if(this.categoryTemplate.templateItemFormList!=null &&this.categoryTemplate.templateItemFormList!=undefined) {
                        this.categoryTemplate.templateItemFormList.forEach(item => {
                                // item.fieldvalue = this.formData4[item.fieldname];
                                this.formData4[item.fieldname] = item.fieldvalue;
                            }
                        );
                    }
                }else{
                    _this.categoryTemplate = this.emptyTemplate;
                }
                if (row !== null) {
                    this.inputData = [];
                    this.formData = Object.assign({}, row);

                    if (
                        row.inventorylist != null &&
                        row.inventorylist[0].inputdata != null
                    ) {
                        console.info("inputdate", row.inventorylist[0].inputdata);
                        this.inputData = JSON.parse(row.inventorylist[0].inputdata);
                        console.info(this.inputData);
                        /*      this.inputData.sort(function (a,b) {
                                  return(a<b)
                              })*/
                    } else {
                        let data = {
                            key: "",
                            val: ""
                        };
                        this.inputData.push(data);
                    }
                }
                let data = [];
                let pic = [];
                let vdata = [];
                let vpic = [];
                for (let i = 0; i < row.commoditypiclist.length; i++) {
                    let picdata = row.commoditypiclist[i];
                    if (picdata.recoursetype === 0) {
                        let obj = {
                            name: "",
                            url: ""
                        };
                        obj.name = i;
                        obj.url = picdata.picurl;
                        data.push(obj);
                        pic.push(picdata.picurl);
                    }
                    if (picdata.recoursetype === 1) {
                        let obj = {
                            name: "",
                            url: ""
                        };
                        obj.name = i;
                        obj.url = picdata.picurl;
                        vdata.push(obj);
                        vpic.push(picdata.picurl);

                        //vurl=picdata.videopic;
                    }
                }

                this.formData.videopic = vpic;
                this.vuploadData = vdata;
                this.commoditypiclist = pic;
                this.formData.commoditypiclist = pic;
                this.uploadData = data;

                // alert(JSON.stringify(row));

                this.parameterlist = row.inventorylist;
                console.info("parameterlist111111", this.parameterlist);
                for (let i = 0; i < this.parameterlist.length; i++) {
                    let sav = [];
                    this.$set(this.parameterlist[i], "savparameterlist", sav);
                    this.$set(this.parameterlist[i], "splist", sav);

                    console.info("pppppppp", this.parameterlist[i].inventory);
                    var a = this.parameterlist[i].inventory.indexOf("}");
                    var b = this.parameterlist[i].inventory.substring(1, a);
                    var a1 = b.split(",");
                    var splist1 = [];
                    for (let c = 0; c < a1.length; c++) {
                        var s = a1[c]
                            .split(":")[1]
                            .substring(1, a1[c].split(":")[1].length - 1);
                        splist1.push(s);
                    }
                    //县域百分比回显
                    if( this.parameterlist[i].profitproportion<1){
                        (this.parameterlist[i].profitproportion)*= 100;
                    }else{
                        (this.parameterlist[i].profitproportion)
                    }
                    this.parameterlist[i].splist = splist1;
                    this.parameterlist[i].savparameterlist = JSON.parse(
                        this.parameterlist[i].secinventory
                    );

                    this.parameterlist[i].companytype = this.parameterlist[
                        i
                        ].companytype.toString();
                    // this.parameterlist[i].splist=splist1;
                    // console.info("qqqqqqqqqqqqq",this.parameterlist[i].splist)
                    // this.ref.sel.$forceUpdate();
                }

                //console.info("99999999999999",this.inputData);
                /*------------------*/
                //参数
                let sav = [];
                if (this.parameterlist != null && this.parameterlist.length > 0) {
                    for (let i = 0; i < this.parameterlist.length; i++) {
                        let pdata = JSON.parse(this.parameterlist[i].secinventory);

                        console.info(pdata);

                        for (let i = 0; i < pdata.length; i++) {
                            let mdata = pdata[i];
                            for (let v in mdata) {
                                // let data = {
                                //     key: v,
                                //     val: mdata[v].toString()
                                // };
                                sav.push(mdata[v].toString());
                            }
                        }
                        this.parameterlist.savparameterlist = pdata;
                    }
                    this.parameterlist.splist = sav;
                }

                this.formName = "edit";
                /*三级*/
                for (
                    var k = 0;
                    k < this.busCommodityThirdCategoryList.length;
                    k++
                ) {
                    if (
                        this.busCommodityThirdCategoryList[k].thirdcategoryid ==
                        this.formData.thirdcategoryid
                    ) {
                        this.ThirdCategoryobj = this.busCommodityThirdCategoryList[
                            k
                            ];

                        this.formData.thirdcategoryname = this.busCommodityThirdCategoryList[
                            k
                            ].thirdcategoryname;

                        break;
                    }
                }
            },
            //编辑库存量弹框
            inventoryForm(index, row) {
                console.info('---row--Data:'+ JSON.stringify(row));
                this.inventoryVisible = true;
                if (row !== null) {
                    this.inputData=[]
                    this.formData = Object.assign({}, row);
                    this.formData.secondarycategoryid=row.secondarycategoryid;
                    this.formData.thirdcategoryid=row.thirdcategoryid;
                    let id=row.warehouseid
                    this.formData.warehouseid=id;
                    let  postagetype=row.postagetype+"";
                    this.formData.postagetype=postagetype;
                    // this.formData.commoditytype=row.commoditytype+"";
                    if(row.inventorylist!=null&&row.inventorylist[0].inputdata!=null){
                        this.inputData=JSON.parse(row.inventorylist[0].inputdata);
                    }else{
                        let data={
                            key:"",
                            val:""
                        }
                        this.inputData.push(data);
                    }
                }
                let data=[];
                let pic=[];
                let vdata=[];
                let vpic=[];
                for (let i=0;i<row.commoditypiclist.length;i++){
                    let picdata=row.commoditypiclist[i];
                    if(picdata.recoursetype===0){
                        let obj={
                            name:"",
                            url:""
                        }
                        obj.name=i;
                        obj.url=picdata.picurl;
                        data.push(obj);
                        pic.push(picdata.picurl)
                    }
                    if(picdata.recoursetype===1){

                        let obj={
                            name:"",
                            url:""
                        }
                        obj.name=i;
                        obj.url=picdata.picurl;
                        vdata.push(obj);
                        vpic.push(picdata.picurl)
                    }
                }

                this.formData.videopic=vpic;
                this.vuploadData=vdata
                this.commoditypiclist=pic;
                this.formData.commoditypiclist=pic;
                this.uploadData=data;

                this.parameterlist=row.inventorylist;
                for(let i=0;i<this.parameterlist.length;i++){
                    let sav=[];
                    this.$set(this.parameterlist[i], "savparameterlist", sav);
                    this.$set(this.parameterlist[i], "splist", sav);

                    var a=this.parameterlist[i].inventory.indexOf("}");
                    var b=this.parameterlist[i].inventory.substring(1,a)
                    var a1=b.split(",");
                    var splist1=[];
                    for (let c=0;c<a1.length;c++) {
                        var s=a1[c].split(":")[1].substring(1,a1[c].split(":")[1].length-1);
                        splist1.push(s)
                    }
                    this.parameterlist[i].splist=(splist1);
                    this.parameterlist[i].savparameterlist=JSON.parse(this.parameterlist[i].secinventory);

                    this.parameterlist[i].companytype= this.parameterlist[i].companytype.toString();
                }
                //参数
                let sav=[];
                if(this.parameterlist!=null&&this.parameterlist.length>0){

                    for(let i=0;i<this.parameterlist.length;i++){
                        let pdata=JSON.parse(this.parameterlist[i].secinventory);

                        console.info(pdata)

                        for (let i=0;i<pdata.length;i++) {
                            let mdata=pdata[i]
                            for (let v in mdata){
                                let data={
                                    key:v,
                                    val:mdata[v].toString()
                                }
                                sav.push(mdata[v].toString());
                            }
                        }
                        this.parameterlist.savparameterlist=pdata;
                    }
                    this.parameterlist.splist=sav;
                }
                this.formName = "inventoryEdit";
                console.info('--parameterlist-data:'+ JSON.stringify(this.parameterlist));
            },
            //添加参数
            addpara() {
                let param = {
                    inventoryid: "", //          bigint not null auto_increment,
                    commodityid: "", //          bigint,
                    inventory: "", //      规格参数      text,
                    secinventory: "", //规格参数2
                    commodityprice: 0, //    分销价   decimal(20,2),
                    commodityoldprice: 0, //  零售价  decimal(20,2),
                    primaryagentprice: 0, //代理价格 新增的 之前是注释的
                    secondaryagentprice: 0, //二级代理
                    costprice: 0, //成本价
                    inventorynum: 0, //      库存   int,
                    inventorypic: "", //   规格图片      varchar(200),
                    companytype: 0, //      单位    smallint comment '0:瓶 1箱 2:套',
                    boxnum: "", //      每箱瓶数         int comment '每箱瓶数',
                    savparameterlist: [],
                    splist: []
                };
                let p = {
                    key: "",
                    val: ""
                };
                param.savparameterlist.push(p);
                this.parameterlist.push(param);
                console.info("++" + this.parameterlist[0].savparameterlist.val);
            },
            //删除
            paraDel(index, row) {
                console.log(row);
                this.parameterlist.splice(index, 1);
            },
            //筛选上架
            shelfstatus() {
                //  alert(this.query.onshelfstatus);
                if (this.query.onshelfstatus === "0") {
                    this.$router.push({
                        path: "",
                    });
                    //刷新事件需要重置查询条件
                    this.createTime = [];
                    this.query = {
                        currentPage: 1,
                        pageSize: 10,
                    };
                    this.getList();
                } else {
                    this.query.currentPage = 1;
                    this.$router.push({
                        path: "",
                        query: this.query,
                    });
                    this.getList();
                }
            },
            //上架
            onshelfschange(row) {
                this.loading = true;
                var params = new URLSearchParams();
                params.append("commodityid", row.commodityid);
                params.append("onshelfstatus", row.onshelfstatus);
                updateBusCommodity(params)
                    .then((response) => {
                        this.loading = false;
                        if (response.code) {
                            this.$message.error(response.message);
                            return false;
                        }
                        this.loading = false;
                        this.$message.success("更新成功");
                        // 刷新数据
                    })
                    .catch(() => {
                        this.loading = false;
                    });
            },
            // 显示表单
            handleForm() {
                this.categoryTemplate = [];
                this.formVisible = true;
                this.formName = "add";
                this.inputData = [];
                this.parameterlist = [];
                this.uploadData = [];
                //this.commoditydesc=""//this.$refs.tinymce.setContent("");
                this.formData = {
                    commodityid: "", //商品id
                    commodityname: "", //商品名称
                    recommendstatus: 0, //商品'0不推荐1推荐',
                    onshelfstatus: 1, //商品 0全部，  1不上架 2上架',
                    commodityicon: "", //商品图片
                    commodityprice: "", // 售价    decimal(20,2),
                    commodityoldprice: "", //商品原价    decimal(20,2),
                    commodityfrom: "", // 货源地
                    topcategoryname: "", //一级分类
                    secondarycategoryname: "", //二级分类
                    thirdcategoryname: "", //三级分类
                    primaryentitybillie: "",
                    seniorentitybillie: "",
                    commoditytype: "",
                    commoditynum: "", // 库存
                    createtime: "", // 添加时间
                    commoditydesc: "", //商品描述
                    commoditypiclist: [], //图片
                    piclist: "", //图片列表
                    videopic: "", //视频地址
                    topcategoryid: "", //一级分类id',
                    secondarycategoryid: "", //二级分类id',
                    thirdcategoryid: "", // 三级分类id,

                    inids: "", //参数id
                    param: "",
                    inputdata: "" //全部参数
                };
            },
            //上传图片
            handleSuccess(response, file) {
                this.isSelect = false;
                console.log("上传成功");
                if (response.status === 1) {
                    const filePath = response.data;
                    this.formData.topcategorypic = filePath;
                    console.info(filePath + file);
                    //  alert(JSON.stringify(this.uploadData));
                    return;
                }

                this.$message.error("上传文件报错");
            },
            handleChange(file) {
                this.fileName = file.name ? file.name : "";
                return false;
            },

            /*获取列表事件*/
            brandCommodityList() {
                this.loading = true;
                //   getTopList
                brandAgentCommodityList(this.query)
                    .then(response => {
                        if (response.code != 0) {
                            this.$message.error(response.message);
                            this.loading = false;
                            return false;
                        }
                        let datalist = response.data.list || [];
                        if (datalist.length > 0) {
                            let mylist = datalist;
                            for (var i = 0; i < mylist.length; i++) {
                                if (mylist[i].onshelfstatus != null) {
                                    mylist[i].onshelfstatus = mylist[i].onshelfstatus.toString();
                                }
                                if (mylist[i].recommendstatus != null) {
                                    mylist[i].recommendstatus = mylist[
                                        i
                                        ].recommendstatus.toString();
                                }
                            }
                            this.list = mylist;

                            console.info(this.list);
                        }
                        this.totalNumber = response.data.totalNumber || 0;
                        setTimeout(() => {
                            this.loading = false;
                        }, 0.3 * 1000);
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });
            },

            /*新增或者编辑提交事件*/
            formSumbit() {
                //表单校验通过
                let ids = this.commodityIdArr.toString();
                this.formData.ids = ids;
                console.info(this.formData);
                this.$refs["ruleForm"].validate(valid => {
                    // console.log(valid)
                    if (valid) {
                        this.formSumbitLoding = true;
                        this.formData.inputdata = JSON.stringify(this.formData);
                        addRepertory(this.formData)
                            .then(response => {
                                this.formSumbitLoding = false;
                                if (response.code != 0) {
                                    this.$message.error(response.message);
                                    return false;
                                }
                                this.merchantsgoodslist = [];
                                this.checkTotalNumber = 0;
                                this.$message.success("操作成功");
                                this.hideDialogAddOrEdit();
                                // 加载表格数据
                                this.brandCommodityList();
                            })
                            .catch(() => {
                                this.formSumbitLoding = false;
                            });
                    }
                });
                //new URLSearchParams(this.query)  提交数据格式不正确时可以转一下
            },

            closeCheck() {
                this.dialogAddOrEdit = false;

                this.merchantsgoodslist = [];
                this.checkTotalNumber = 0;
            },
            /*列表查询条件事件*/
            onSubmit() {
                this.query.currentPage = 1;
                this.$router.push({
                    path: "",
                    query: this.query,
                });
                //this.getList();
                this.brandCommodityList();
            },

            /*刷新*/
            onRest() {
                //刷新事件需要重置查询条件
                this.query = {
                    currentPage: 1,
                    pageSize: 10
                };
                this.brandCommodityList();
            },
            // 隐藏详情弹框
            hideDetailForm() {
                // 更改值
                this.detailVisible = !this.detailVisible;
                return true;
            },

            //显示 查看弹框
            detailForm(index, row) {
                this.detailVisible = true;
                if (row !== null) {
                    this.inputData=[]
                    this.formData = Object.assign({}, row);
                    console.info(row)
                    console.info(this.formData )
                    this.formData.secondarycategoryid=row.secondarycategoryid;
                    this.formData.thirdcategoryid=row.thirdcategoryid;
                    let id=row.warehouseid
                    this.formData.warehouseid=id;
                    let  postagetype=row.postagetype+"";
                    this.formData.postagetype=postagetype;
                    // this.formData.commoditytype=row.commoditytype+"";
                    if(row.inventorylist!=null&&row.inventorylist[0].inputdata!=null){
                        this.inputData=JSON.parse(row.inventorylist[0].inputdata);
                    }else{
                        let data={
                            key:"",
                            val:""
                        }
                        this.inputData.push(data);
                    }
                    if (row.certificationcontent != null) {
                        this.categoryTemplate = JSON.parse(row.certificationcontent);
                        this.formData.certificationcontent = JSON.parse(
                            row.certificationcontent
                        );
                        if(this.categoryTemplate.templateItemFormList!=null &&this.categoryTemplate.templateItemFormList!=undefined) {
                            this.categoryTemplate.templateItemFormList.forEach((item) => {
                                // item.fieldvalue = this.formData4[item.fieldname];
                                this.formData4[item.fieldname] = item.fieldvalue;
                            });
                        }
                    }
                }
                let data=[];
                let pic=[];
                let vdata=[];
                let vpic=[];
                for (let i=0;i<row.commoditypiclist.length;i++){
                    let picdata=row.commoditypiclist[i];
                    if(picdata.recoursetype===0){
                        let obj={
                            name:"",
                            url:""
                        }
                        obj.name=i;
                        obj.url=picdata.picurl;
                        data.push(obj);
                        pic.push(picdata.picurl)
                    }
                    if(picdata.recoursetype===1){

                        let obj={
                            name:"",
                            url:""
                        }
                        obj.name=i;
                        obj.url=picdata.picurl;
                        vdata.push(obj);
                        vpic.push(picdata.picurl)
                    }
                }

                this.formData.videopic=vpic;
                this.vuploadData=vdata
                this.commoditypiclist=pic;
                this.formData.commoditypiclist=pic;
                this.uploadData=data;

                this.parameterlist=row.inventorylist;
                for(let i=0;i<this.parameterlist.length;i++){
                    let sav=[];
                    this.$set(this.parameterlist[i], "savparameterlist", sav);
                    this.$set(this.parameterlist[i], "splist", sav);

                    var a=this.parameterlist[i].inventory.indexOf("}");
                    var b=this.parameterlist[i].inventory.substring(1,a)
                    var a1=b.split(",");
                    var splist1=[];
                    for (let c=0;c<a1.length;c++) {
                        var s=a1[c].split(":")[1].substring(1,a1[c].split(":")[1].length-1);
                        splist1.push(s)
                    }
                    //县域百分比回显
                    if( this.parameterlist[i].profitproportion<1){
                        (this.parameterlist[i].profitproportion)*= 100;
                    }else{
                        (this.parameterlist[i].profitproportion)
                    }
                    this.parameterlist[i].splist=(splist1);
                    this.parameterlist[i].savparameterlist=JSON.parse(this.parameterlist[i].secinventory);

                    this.parameterlist[i].companytype= this.parameterlist[i].companytype.toString();
                }
                //参数
                let sav=[];
                if(this.parameterlist!=null&&this.parameterlist.length>0){

                    for(let i=0;i<this.parameterlist.length;i++){
                        let pdata=JSON.parse(this.parameterlist[i].secinventory);

                        console.info(pdata)

                        for (let i=0;i<pdata.length;i++) {
                            let mdata=pdata[i]
                            for (let v in mdata){
                                sav.push(mdata[v].toString());
                            }
                        }
                        this.parameterlist.savparameterlist=pdata;
                    }
                    this.parameterlist.splist=sav;
                }
                this.formName = "detail";
            },

            /*显示 -新增或者编辑弹框*/
            showDialogAddOrEdit(index, row) {
                let { href } = this.$router.resolve({ path: '/merchant/mycommodity',query: {p: 'add'} })
                window.open(href, '_blank')
                // this.formData = Object.assign({}, formJson);
                // this.dialogAddOrEdit = true;
                // this.dialogAddOrEditName = "编辑";

                // this.$router.push("addBrandAgentCommodity");
            },

            addBrandCommodity(){
                // this.$router.push({ path: '/merchant/mycommodity', query: {p: 'add'} })
                this.$router.push("addBrandAgentCommodity")
            },

            /*隐藏 -新增或编辑弹框*/
            hideDialogAddOrEdit() {
                this.dialogAddOrEdit = false;
                this.merchantsgoodslist = [];
                //隐藏弹框时需要清除弹框内容
                if (this.$refs["ruleForm"]) {
                    this.$refs["ruleForm"].resetFields(); //清除formData表单
                    this.$refs["ruleForm"].clearValidate(); //清除表单验证
                }
                //如果上传文件，还需要清除文件列表
            },
            getMerchantsLIst() {
                let query = {
                    currentPage: 1,
                    pageSize: 1000
                };
                merchantsAgentList(query)
                    .then(response => {
                        if (response.code != 0) {
                            this.$message.error(response.message);
                            return false;
                        }
                        console.log("merchantsList======"+response.data.list);
                        this.merchantslist = response.data.list || [];
                    })
                    .catch(() => {
                        this.merchantslist = [];
                    });
            },
            // 弹窗下拉选项变更获取商品
            getMerchantsGoods() {
                this.merchantsgoodslist = [];
                this.formData.ids = "";
                this.checkQuery.userId = this.formData.userId;
                console.info(this.checkQuery);
                this.checkLoading = true;
                getMerchantsGoods(this.checkQuery)
                    .then(response => {
                        console.log(response);
                        if (response.code != 0) {
                            this.$message.error(response.message);
                            return false;
                            this.checkLoading = false;
                        }
                        this.checkLoading = false;
                        this.checkTotalNumber = response.data.totalNumber || 0;
                        this.merchantsgoodslist = response.data.list || [];
                        console.log(this.merchantsgoodslist);
                    })
                    .catch(() => {
                        this.checkLoading = false;
                        this.merchantsgoodslist = [];
                    });
            },

            /*点击页数*/
            handleCurrentChange(val) {
                this.query.currentPage = val;
                this.brandCommodityList();
            },

            /*选择每页条数【10,20,30,40,50】*/
            handleSizeChange(val) {
                this.query.pageSize = val;
                this.brandCommodityList();
            },

        },
        created() {
            //this.getList();
            //this.getAllCategory();
            this.brandCommodityList();
            this.getAllCategory();

            // this.getMerchantsLIst();
        }
    };
</script>
